package com.llt.retry.service;

import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

/**
 * @author llt
 * @date 2025/5/20
 */
@Service
public class RemoteService {

    private int attempt = 0;

    @Retryable(
            value = { RuntimeException.class }, // 哪些异常重试
            maxAttempts = 3,                    // 最多重试3次（包括第一次）
            backoff = @Backoff(delay = 2000)    // 每次重试间隔2秒
    )
    public void callRemoteApi() {
        attempt++;
        System.out.println("远程调用服务，尝试次数：" + attempt);
        // 模拟失败
        throw new RuntimeException("远程调用失败");
    }

    @Recover
    public void recover(RuntimeException e) {
        System.out.println("重试失败，执行兜底逻辑: " + e.getMessage());
    }
}
